<template>
	<view v-if="!isFirstload" class="container">
		<!-- 页面头部 -->
		<view class="main-header"
			:style="{ height: platform == 'H5' ? '260rpx' : '500rpx', paddingTop: platform == 'H5' ? '0' : '80rpx' }">
			<!-- <image class="bg-image" src="/static/background/user-header2.png" mode="scaleToFill"></image> -->
			<!-- 用户信息 -->
			<view v-if="isLogin" class="user-info">
				<view class="user-avatar">
					<avatar-image :url="userInfo.avatar_url" :width="100" />
				</view>
				<view class="user-content">
					<!-- 会员昵称 -->
					<view class="nick-name oneline-hide">{{ userInfo.nick_name }}</view>
					<!-- 会员等级 -->
					<view v-if="userInfo.grade_id > 0 && userInfo.grade" class="user-grade">
						<view class="user-grade_icon">
							<image class="image"
								src="">
							</image>
						</view>
						<view class="user-grade_name">
							<text>{{ userInfo.grade.name }}</text>
						</view>
					</view>
					<!-- 会员无等级时显示手机号 -->
					<view v-else class="mobile">{{ userInfo.mobile }}</view>
				</view>
			</view>
			<!-- 未登录 -->
			<view v-else class="user-info" @click="handleLogin">
				<view class="user-avatar">
					<avatar-image :width="100" />
				</view>
				<view class="user-content">
					<view class="nick-name">未登录</view>
					<view class="login-tips">点击登录账号</view>
				</view>
			</view>
		</view>
		<view class="list flex flex-x-a">
			<view class="b_1" @click="about">
				关于我们
			</view>
			<view class="b_2" @click="callPhone">
				联系客服
			</view>
		</view>
		<view class="info_list">
			<view class="postList">
				<view class="myPost">
					我的发布
				</view>
				<view class="p_l">
					<view class="p_tab">
						<view @click="changeShow" class=" flex flex-y-c flex-x-c" :class="[show?'p_t_1':'p_t_2']">
							<image src="../../static/4.png" mode=""></image>出（卖）
						</view>
						<view @click="changeShow" class=" flex flex-y-c flex-x-c" :class="[!show?'p_t_1':'p_t_2']">
							<image src="../../static/2.png" mode=""></image>
							求（买）
						</view>
					</view>
					<!-- <view class="empty_img">
						<image src="/static/empty.png" mode=""></image>
						<view class="no-content">
							暂无内容
						</view>
					</view> -->
					<view class="p_t_list">
						<view class="flex">
							<view class="p_img">
								<image src="../../static/wallet.png" mode=""></image>
							</view>
							<view class="m-l w-400">
								<view class="flex flex-x-between">
									<view class="alcohol_name">
										茅台不老酒
									</view>
									<view class="alcohol_price">
										￥100
									</view>
								</view>
								<view class="flex m-t-b-tag">
									<view class="tag1">
										品牌：茅台
									</view>
									<view class="tag2">
										数量：1
									</view>
								</view>
								<view class="flex flex-y-c">
									<view class="alcohol_city">
										<image src="../../static/6.png" mode=""></image>
										太原市
									</view>
									<view class="alcohol_time">
										<image src="../../static/7.png" mode=""></image>
										2022-12-02
									</view>
								</view>
							</view>
						</view>
						<view class="line"></view>
						<view class="flex j-f-e">
							<view class="flex flex-y-c f-s-n" @click="edit">
								<image src="/static/edit.png" style="width: 36rpx;height: 38rpx;margin-right: 15rpx;" mode=""></image>
								<view class="" style="margin-right: 15rpx;">
									编辑
								</view>
							</view>
							<view class="flex flex-y-c f-s-n">
								<image src="/static/del.png" style="width: 36rpx;height: 38rpx;margin-right: 15rpx;" mode=""></image>
								<view class="">
									删除
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 退出登录 -->
		<view v-if="isLogin" class="my-logout">
			<view class="logout-btn" @click="handleLogout()">
				<text>退出登录</text>
			</view>
		</view>

	</view>
</template>

<script>
	import store from '@/store'
	import AvatarImage from '@/components/avatar-image'
	import {
		setCartTabBadge
	} from '@/core/app'
	import SettingKeyEnum from '@/common/enum/setting/Key'
	import SettingModel from '@/common/model/Setting'
	import * as UserApi from '@/api/user'
	import * as OrderApi from '@/api/order'
	import {
		checkLogin
	} from '@/core/app'

	export default {
		components: {
			AvatarImage
		},
		data() {
			return {
				// 枚举类
				SettingKeyEnum,
				// 正在加载
				isLoading: true,
				// 首次加载
				isFirstload: true,
				// 是否已登录
				isLogin: false,
				// 系统设置
				setting: {},
				// 当前用户信息
				userInfo: {},
				// 账户资产
				assets: {
					balance: '--',
					points: '--',
					coupon: '--'
				},
				// 当前用户待处理的订单数量
				todoCounts: {
					payment: 0,
					deliver: 0,
					received: 0
				},
				
				show:true
			}
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow(options) {
			this.onRefreshPage()
		},

		methods: {
			about(){
				uni.navigateTo({
					url:'/pages/about/index'
				})
			},
			edit(){
				uni.navigateTo({
					url:'/pages/edit/index'
				})
			},
			callPhone(){
				uni.makePhoneCall({
					phoneNumber:'13232'
				})
			},
			// 刷新页面
			onRefreshPage() {
				// 更新购物车角标
				setCartTabBadge()
				// 判断是否已登录
				this.isLogin = checkLogin()
				// 获取页面数据
				this.getPageData()
			},

			// 获取页面数据
			getPageData(callback) {
				const app = this
				app.isLoading = true
				Promise.all([app.getSetting(), app.getUserInfo(), app.getUserAssets(), app.getTodoCounts()])
					.then(result => {
						app.isFirstload = false
						// 初始化我的服务数据
						// 执行回调函数
						callback && callback()
					})
					.catch(err => console.log('catch', err))
					.finally(() => app.isLoading = false)
			},
			// 获取商城设置
			getSetting() {
				const app = this
				return new Promise((resolve, reject) => {
					SettingModel.data()
						.then(setting => {
							app.setting = setting
							resolve(setting)
						}).catch(reject)
				})
			},

			// 获取当前用户信息
			getUserInfo() {
				const app = this
				return new Promise((resolve, reject) => {
					!app.isLogin ? resolve(null) : UserApi.info({}, {
							load: app.isFirstload
						})
						.then(result => {
							app.userInfo = result.data.userInfo
							resolve(app.userInfo)
						})
						.catch(err => {
							if (err.result && err.result.status == 401) {
								app.isLogin = false
								resolve(null)
							} else {
								reject(err)
							}
						})
				})
			},

			// 获取账户资产
			getUserAssets() {
				const app = this
				return new Promise((resolve, reject) => {
					!app.isLogin ? resolve(null) : UserApi.assets({}, {
							load: app.isFirstload
						})
						.then(result => {
							app.assets = result.data.assets
							resolve(app.assets)
						})
						.catch(err => {
							if (err.result && err.result.status == 401) {
								app.isLogin = false
								resolve(null)
							} else {
								reject(err)
							}
						})
				})
			},

			// 获取当前用户待处理的订单数量
			getTodoCounts() {
				const app = this
				return new Promise((resolve, reject) => {
					!app.isLogin ? resolve(null) : OrderApi.todoCounts({}, {
							load: app.isFirstload
						})
						.then(result => {
							app.todoCounts = result.data.counts
							resolve(app.todoCounts)
						})
						.catch(err => {
							if (err.result && err.result.status == 401) {
								app.isLogin = false
								resolve(null)
							} else {
								reject(err)
							}
						})
				})
			},

			// 跳转到登录页
			handleLogin() {
				!this.isLogin && this.$navTo('pages/login/index')
			},

			// 退出登录
			handleLogout() {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '您确定要退出登录吗?',
					success(res) {
						if (res.confirm) {
							store.dispatch('Logout', {})
								.then(result => app.onRefreshPage())
						}
					}
				})
			},

			// 跳转到钱包页面
			onTargetWallet() {
				this.$navTo('pages/wallet/index')
			},

			// 跳转到订单页
			onTargetOrder(item) {
				this.$navTo('pages/order/index', {
					dataType: item.id
				})
			},

			// 跳转到我的积分页面
			onTargetPoints() {
				this.$navTo('pages/points/log')
			},

			// 跳转到我的优惠券页
			onTargetMyCoupon() {
				this.$navTo('pages/my-coupon/index')
			},

			// 跳转到服务页面
			handleService({
				url
			}) {
				this.$navTo(url)
			},
			// tab切换
			changeShow(){
				this.show=!this.show
			}

		},

		/**
		 * 下拉刷新
		 */
		onPullDownRefresh() {
			// 获取首页数据
			this.getPageData(() => {
				uni.stopPullDownRefresh()
			})
		},
		
		
		


	}
</script>

<style lang="scss" scoped>
	// 页面头部
	.main-header {
		background: rgba(49, 121, 236, 0.74);
		// background-image: url('/static/background/user-header.png');
		position: relative;
		width: 100%;
		height: 280rpx;
		background-size: 100% 100%;
		overflow: hidden;
		display: flex;
		align-items: center;
		// padding-top: 40rpx;
		padding-left: 30rpx;

		.bg-image {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 0;
		}

		.user-info {
			display: flex;
			height: 100rpx;
			z-index: 1;

			.user-content {
				display: flex;
				flex-direction: column;
				justify-content: center;
				margin-left: 30rpx;
				color: #fff;

				.nick-name {
					font-size: 34rpx;
					font-weight: bold;
					max-width: 270rpx;
				}

				.mobile {
					margin-top: 15rpx;
					font-size: 28rpx;
				}

				.user-grade {
					align-self: baseline;
					display: flex;
					align-items: center;
					background: #3c3c3c;
					margin-top: 12rpx;
					border-radius: 10rpx;
					padding: 4rpx 12rpx;

					.user-grade_icon .image {
						display: block;
						width: 32rpx;
						height: 32rpx;
					}

					.user-grade_name {
						margin-left: 5rpx;
						font-size: 26rpx;
						color: #EEE0C3;
					}

				}

				.login-tips {
					margin-top: 12rpx;
					font-size: 30rpx;
				}

			}
		}
	}

	// 角标组件
	.item-badge {
		position: absolute;
		top: 0;
		right: 55rpx;
		background: #fa2209;
		color: #fff;
		border-radius: 100%;
		min-width: 38rpx;
		height: 38rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 1rpx;
		font-size: 24rpx;
	}

	// 我的钱包
	.my-asset {
		display: flex;
		background: #fff;
		padding: 40rpx 0;

		.asset-right {
			width: 200rpx;
			border-left: 1rpx solid #eee;
		}

		.asset-right-item {
			text-align: center;
			color: #545454;

			.item-icon {
				font-size: 44rpx;
			}

			.item-name {
				margin-top: 14rpx;
				font-size: 28rpx;
			}

		}

		.asset-left-item {
			text-align: center;
			color: #666;
			padding: 0 42rpx;

			.item-value {
				font-size: 34rpx;
				color: red;
			}

			.item-name {
				margin-top: 6rpx;
			}

			.item-name {
				margin-top: 14rpx;
				font-size: 28rpx;
			}
		}

	}

	// 订单操作
	.order-navbar {
		display: flex;
		margin: 20rpx auto 20rpx auto;
		padding: 20rpx 0;
		width: 94%;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		font-size: 30rpx;
		border-radius: 5rpx;
		background: #fff;

		&-item {
			position: relative;
			width: 25%;

			.item-icon {
				text-align: center;
				margin: 0 auto;
				padding: 10rpx 0;
				color: #545454;
				font-size: 44rpx;
			}

			.item-name {
				font-size: 28rpx;
				color: #545454;
				text-align: center;
				margin-right: 10rpx;
			}

		}
	}

	// 我的服务
	.my-service {
		margin: 22rpx auto 22rpx auto;
		padding: 22rpx 0;
		width: 94%;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		border-radius: 5rpx;
		background: #fff;

		.service-title {
			padding-left: 24rpx;
			margin-bottom: 20rpx;
			font-size: 30rpx;
		}

		.service-content {

			margin-bottom: -20rpx;

			.service-item {
				position: relative;
				width: 25%;
				float: left;
				margin-bottom: 30rpx;

				.item-icon {
					text-align: center;
					margin: 0 auto;
					padding: 14rpx 0;
					color: #ff3800;
					font-size: 44rpx;
				}

				.item-name {
					font-size: 28rpx;
					color: #545454;
					text-align: center;
					margin-right: 10rpx;
				}

			}
		}
	}

	// 退出登录
	.my-logout {
		display: flex;
		justify-content: center;
		margin-top: 50rpx;

		.logout-btn {
			width: 60%;
			margin: 0 auto;
			font-size: 28rpx;
			color: #616161;
			border-radius: 20rpx;
			border: 1px solid #dcdcdc;
			padding: 16rpx 0;
			text-align: center;
		}
	}




	.info_list {
		width: 750rpx;
		height: calc(100vh - 466rpx);
		background: #F5F5F5;
		border-radius: 20rpx 20rpx 0 0;
		margin-top: -90rpx;
		position: relative;
		z-index: 9;
	}

	.list {
		margin-top: -80rpx;
		position: relative;
		z-index: 10;
	}
	.j-f-e{
		justify-content: flex-end;
	}
	.b_1 {
		width: 315rpx;
		height: 158rpx;
		background: linear-gradient(0deg, #8797F0 0%, #4F61E6 100%);
		border-radius: 20rpx;
		background-image: url('../../static/13.png');
		background-size: cover;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #FFFFFF;
		text-align: center;
		line-height: 158rpx;
	}

	.b_2 {
		width: 315rpx;
		height: 158rpx;
		background: linear-gradient(0deg, #9D89FF 0%, #835FFF 100%);
		border-radius: 20rpx;
		background-image: url('../../static/14.png');
		background-size: cover;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #FFFFFF;
		text-align: center;
		line-height: 158rpx;
	}

	.flex {
		display: flex;
	}

	.flex-x-a {
		justify-content: space-around;
	}
	.flex-x-between{
		justify-content: space-between;
	}
	.flex-x-c{
		justify-content: center;
	}
	.flex-y-c{
		align-items: center;
	}
	.postList {
		padding: 34rpx;
		padding-top: 80rpx;
	}

	.myPost {
		font-size: 34rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
		line-height: 60rpx;
		margin: 55rpx 0;
	}

	.p_tab {
		display: flex;
		align-items: center;
	}

	.p_t_1 {
		width: 338rpx;
		height: 90rpx;
		background: rgba(0, 80, 208, 0.1);
		border-radius: 20rpx 20rpx 0rpx 0rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
		line-height: 90rpx;
		text-align: center;
	}
	.p_t_1 image{
		width: 45rpx;
		height: 45rpx;
		margin-right: 20rpx;
	}
	.p_t_2 {
		width: 347rpx;
		height: 90rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0px 0px;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
		line-height: 90rpx;
		text-align: center;
	}
	.p_t_2 image{
		width: 45rpx;
		height: 45rpx;
		margin-right: 20rpx;
	}
	.p_t_list {
		padding: 32rpx 14rpx 22rpx 14rpx;
		background-color: #fff;
		margin-top: 28rpx;
	}
	
	.p_img{
		width: 200rpx;
		height: 150rpx;
		background: #FEFEFE;
		border-radius: 10rpx;
	}
	.w-400{
		width: 400rpx;
	}
	.p_img image{
		width: 100%;
		height: 100%;
	}
	.tag1{
		background: rgba(1,100,234,0.1);
		border-radius: 20rpx;
		padding: 10rpx 16rpx;
		margin-right: 24rpx;
		font-size: 20rpx;
		font-family: PingFang SC;
		color: #0164EA;
		text-align: center;
		
	}
	.tag2{
		background: rgba(241,104,76,0.1);
		border-radius: 20rpx;
		padding: 10rpx 16rpx;
		font-size: 20rpx;
		font-family: PingFang SC;
		color: #F1684C;
		text-align: center;
	}
	
	.m-l{
		margin-left: 40rpx;
	}
	
	.m-t-b-tag{
		margin: 15rpx 0;
	}
	
	.alcohol_name{
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #333333;
	}
	
	.alcohol_price{
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
	}
	
	.alcohol_city{
		font-size: 22rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #999999;
		display: flex;
		align-items: center;
		margin-right: 80rpx;
	}
	.alcohol_city image{
		width: 26rpx;
		height: 25rpx;
		margin-right: 20rpx;
	}
	.alcohol_time{
		font-size: 22rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #999999;
		display: flex;
		align-items: center;
	}
	.alcohol_time image{
		width: 26rpx;
		height: 26rpx;
		margin-right: 20rpx;
	}
	
	.line{
		// width: 686rpx;
		height: 1rpx;
		background-color: #F5F5F5;
		margin: 20rpx auto;
	}
	
	
	.f-s-n{
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #999999;
	}
	
	
	.empty_img{
		width: 248rpx;
		height: 169rpx;
		margin: 100rpx auto;
		text-align: center;
	}
	.empty_img image{
		width: 100%;
		height: 100%;
	}
	
	.no-content{
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #848484;
	}
</style>
